<?php

class admin_Model_CategoriaMapper {

    protected $_dbTable;

    public function setDbTable($dbTable) {

        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }

        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Enlace a tabla de datos inválido');
        }

        $this->_dbTable = $dbTable;

        return $this;
    }

    public function getDbTable() {

        if (null === $this->_dbTable) {
            $this->setDbTable('admin_Model_DbTable_Categoria');
        }

        return $this->_dbTable;
    }

    public function obtenerTodos($limite = 0) {

        $select = $this->getDbTable()->select();

        $limite = (int) $limite;

        if ($limite) {
            $select->limit($limite);
        }

        return $this->getDbTable()->fetchAll($select);
    }

    public function obtenerPorId($id) {

        $id = (int) $id;
        return $this->getDbTable()->find($id)->current();
    }

    public function obtenerPorSlug($slug) {

        $select = $this->getDbTable()->select();
        $select->where('slug=?', $slug);
        return $this->getDbTable()->fetchRow($select);
    }

    public function agregar(admin_Model_Categoria $objCategoria) {

        $categoria = $this->getDbTable()->createRow();
        
        $categoria->nombre = $objCategoria->getNombre();
        $categoria->slug = $this->_generarSlug($objCategoria->getSlug());
        $categoria->titulo = $objCategoria->getTitulo();
        $categoria->h1 = $objCategoria->getH1();
        $categoria->descripcion = $objCategoria->getDescripcion();
        $categoria->meta_keywords = $objCategoria->getMetaKeywords();
        $categoria->meta_descripcion = $objCategoria->getMetaDescripcion();
        
        $categoria->save();

    }

    public function editar(admin_Model_Categoria $objCategoria) {

        $categoria = array(
            'nombre' => $objCategoria->getNombre(),
            'slug' => $this->_generarSlug($objCategoria->getSlug()),
            'titulo' => $objCategoria->getTitulo(),
            'h1' => $objCategoria->getH1(),
            'descripcion' => $objCategoria->getDescripcion(),
            'meta_keywords' => $objCategoria->getMetaKeywords(),
            'meta_descripcion' => $objCategoria->getMetaDescripcion()
        );

        $this->getDbTable()->update($categoria, 'id=' . (int) $objCategoria->getId());
    }

    public function eliminar($id) {

        $id = (int) $id;
        $categoria = $this->getDbTable()->find($id)->current();

        if ($categoria !== null) {
            $categoria->delete();
        }

    }

    private function _caracteresLatinos($cadena) {

        //acentos
        $cadena = preg_replace("/(À|Á|Â|Ã|Ä|Å|à|á|â|ã|ä|å)/", "a", $cadena);
        $cadena = preg_replace("/(È|É|Ê|Ë|è|é|ê|ë)/", "e", $cadena);
        $cadena = preg_replace("/(Ì|Í|Î|Ï|ì|í|î|ï)/", "i", $cadena);
        $cadena = preg_replace("/(Ò|Ó|Ô|Õ|Ö|Ø|ò|ó|ô|õ|ö|ø)/", "o", $cadena);
        $cadena = preg_replace("/(Ù|Ú|Û|Ü|ù|ú|û|ü)/", "u", $cadena);
        //la ñ
        $cadena = preg_replace("/(Ñ|ñ)/", "n", $cadena);
        //caracteres extraños
        $cadena = preg_replace("/(Ç|ç)/", "c", $cadena);
        $cadena = preg_replace("/ÿ/", "y", $cadena);

        return $cadena;
    }

    private function _generarSlug($cadena, $separador = '-') {

        $cadena = trim($cadena);
        $cadena = $this->_caracteresLatinos($cadena);
        $cadena = strtolower($cadena);
        $cadena = trim($cadena);
        $cadena = str_replace(" ", $separador, $cadena);
        $cadena = preg_replace("/[\\t\\n\\r]+/", "", $cadena);
        $cadena = preg_replace("/[^ A-Za-z0-9-]/", "", $cadena);

        return $cadena;
    }

}

